<?php

declare(strict_types=1);

namespace app\admin\service;

use app\admin\common\ServiceResponse;
use app\admin\model\Partner as PartnerModel;
use app\admin\model\Images as ImagesModel;
use app\admin\model\Operator as OperatorModel;
use think\facade\Db;

class Partner extends \think\Service
{

    use ServiceResponse;

    protected $return;
    protected $logger;
    protected $validate;
    protected $model;
    public function __construct()
    {
        // $this->logger = new CustomerLogService();
        $this->model = new PartnerModel();
        $this->return = [
            'code' => 500001,
            'data' => [],
            'message' => ''
        ];
    }

    /**
     * 注册服务
     *
     * @return mixed
     */
    public function register()
    {
        //
    }

    /**
     * 执行服务
     *
     * @return mixed
     */
    public function boot()
    {
        //
    }


    /**
     * @describe 获取一条数据
     * @author: Bobo
     * @date: 2024-12-23 11:27:41 
     * @param [int] @required $fvid 主键
     * @return Renderable
     */
    public function getOne(int $id)
    {
        $res = $this
            ->model
            ->field('id,name,image,link,description,sort,is_del,create_time,creator,update_time,updater')
            ->where(['id' => $id])
            ->find()->toArray();

        $imageModel = new ImagesModel();
        $res['image_list'] = $imageModel->getImagesByIds($res['image']);

        $creatorModel = new OperatorModel();
        $res['creator_name'] = $creatorModel->where('id', $res['creator'])->value('cname');
        $res['updater_name'] = $creatorModel->where('id', $res['updater'])->value('cname');

        return $this->success($res);
    }
    /**
     * @describe 获取列表 分页
     * @author: Bobo
     * @date: 2024-12-19 18:20:16 
     * @param [int] $page 
     * @param [int] $limit 
     * @param [array] $where 
     * @return Renderable
     */
    public function getList(int $page = 1, int $limit = 10, array $where = [])
    {
        $query = $this->model->where($where);
        $total = $query->count();
        $creatorArr = array_unique(array_filter($query->column('creator')));

        $creatorList = Db::name('operator')->whereIn('id', $creatorArr)->field('id,cname')->select()->toArray();

        $list = $query
            ->field('id,name,image,link,description,sort,is_del,create_time,creator,update_time,updater')
            ->order('sort asc ,id desc')
            ->page($page, $limit)
            ->select()
            ->toArray();

        $imageIds = array_column($list, 'image');
        $imageModel = new ImagesModel();
        $imageList = $imageModel->getImagesByIds($imageIds);

        $imageList = array_column($imageList, null, 'id');

        $list = array_map(function ($item) use ($imageList) {
            $item['image_url'] = $imageList[$item['image']]['url'] ?? [];
            return $item;
        }, $list);

        $creatorIds = array_column($creatorList, 'id');
        $creatorNames = array_column($creatorList, 'cname');
        $list = array_map(function ($item) use ($creatorIds, $creatorNames) {
            $item['creator'] = in_array($item['creator'], $creatorIds) ? $creatorNames[array_search($item['creator'], $creatorIds)] : '';
            $item['updater'] = in_array($item['updater'], $creatorIds) ? $creatorNames[array_search($item['updater'], $creatorIds)] : '';
            return $item;
        }, $list);

        $data = ['list' => $list, 'total' => $total];
        return $this->success($data);
    }

    /**
     * @describe 新增固件版本
     * @author: Bobo
     * @date: 2024-12-20 10:56:35 
     * @param [arr] @required $data 新增数据
     * @return Renderable
     */
    public function save(array $data)
    {
        if ($this->model->where('name', $data['name'])->find()) {
            return $this->error(100004);
        }

        $schema = $this->model->getSchema();
        $data = array_intersect_key($data, $schema);

        $res = $this->model->save($data);
        if ($res) {
            return $this->success();
        } else {
            return $this->error(500001);
        }
    }

    /**
     * @describe
     * @author: Bobo
     * @date: 2024-12-23 12:36:11 
     * @param [type] @required $data 要修改的数据
     * @param [type] @required $condition 条件
     * @return Renderable 
     */
    public function update(array $data, array $condition)
    {
        $res = $this->model->where($condition)->find();
        if (!$res) {
            return false;
        }

        foreach ($data as $key => $value) {
            if ($res->$key != $value) {
                $res->$key = $value;
            }
        }
        $result = $res->save();
        if ($result) {
            return $this->success();
        } else {
            return $this->error(500001);
        }
    }
}
